package com.itqf.service.impl;


import com.itqf.dao.UserDao;
import com.itqf.dao.impl.UserDaoImpl;
import com.itqf.entity.User;
import com.itqf.service.UserService;
import com.itqf.utils.Constants;
import com.itqf.utils.DbUtils;
import com.itqf.utils.EmailUtils;
import com.itqf.utils.MD5Utils;

import java.sql.SQLException;
import java.util.List;


public class UserServiceImpl implements UserService {


    private static UserDao userDao = new UserDaoImpl();

    /**
     * 判断用户名是否存在
     * @param username  查询条件
     * @return true用户名存在 false用户名不存在
     * @throws SQLException
     */
    @Override
    public boolean checkedUser(String username) {
        //置空对象
        boolean result = false ;
        User user = null;
        try {
            //开启事务
            DbUtils.begin();
            //调用dao层
            user = userDao.selectUserByUname(username);
            //非空判断
            if (user != null) {
                result = true  ;
            }
            //提交业务
            DbUtils.Commit();
        } catch (SQLException e) {
            //出现异常回滚事务
            DbUtils.Rollback();
            e.printStackTrace();
        }
        return result ;
    }

    /**
     * 注册用户业务
     * @param user
     * @return
     */
    @Override
    public int registerUser(User user)  {
        //置空结果对象
        int result = 0 ;
        try {
            //开启事务
            DbUtils.begin();
            int insert = userDao.insert(user);
            if (insert != 0) {
                result = insert ;
            }
            //2.发送一封邮件
            EmailUtils.sendEmail(user);
            //提交事务
            DbUtils.Commit();
        } catch (SQLException e) {
            //出现异常，回滚事务
            DbUtils.Rollback();
            e.printStackTrace();
        }
        return result ;
    }

    @Override
    public int activeUser(String code) throws SQLException {
        //1.根据激活码查找用户
        User user = userDao.selectUserByActiveCode(code);
        if (user == null) {
            return 0 ; //激活失败
        }
        //2.判断用户是否激活
        if (user.getUstatus().equals(Constants.USER_ACTIVE)) {
            return Constants.ACTIVE_ALREADY ;//已经激活
        }
        //3.进行激活操作
        int i = userDao.updateStatusByUid(user.getUid());
        if (i != 0) {
            return Constants.ACTIVE_SUCCESS ; //激活用户
        }
        return Constants.ACTIVE_FAIL ; // 激活失败
    }

    @Override
    public User loginUser(String username , String password) throws SQLException {

        //1.需要将密码用md5处理
        String md5pwd = MD5Utils.md5(password);

        //2.根据用户名，查找用户信息
        User user = userDao.selectUserByUname(username);

        //非空判断
        if (user != null && user.getUpassword().equals(md5pwd)) {
            return user ;
        }
        return user;
    }

    @Override
    public User selectUser(String username, String sex) throws SQLException {

        User user = userDao.selectUser(username ,sex);
        return user;
    }

    @Override
    public List<User> selectAllUser() throws SQLException {

        List<User> userList = userDao.selectAllUser();
        return userList;
    }

    @Override
    public int deleteUserByUid(int uid) throws SQLException {
        int i = userDao.deleteUserByUid(uid);

        return i ;
    }


}
